// pages/resource/resource.js
import { getNextDay } from "../../utils/util";
import { getPreDay } from "../../utils/util";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /**
     * 是否显示甘特图
     */
    enableGant: true,
    /**
     * 是否显示负载图
     */
    enableLoad: false,
    /**
     * 是否显示管理页面
     */
    enableManage: false,
    /**
     * 是否隐藏添加资源框
     */
    hideResAddForm: true,
    /**
     * 甘特图选择的日期
     */
    gantTimeSelected: "2018-10-14",
    /**
     * 负载图选择的日期
     */
    loadTimeSelected: "2018-10-20",
    /**
     * 负载图展示的日期
     */
    loadOnshowWeek: [
      "1-1",
      "1-2",
      "1-3",
      "1-4",
      "1-5",
      "1-6",
      "1-7"
    ],
    /**
     * 输入的资源名
     */
    inputResName: '',
    /**
     * 选择的资源类型
     */
    selectedResType: '班组',
    /**
     * 输入的资源量
     */
    inputResNum: 0,
    /**
     * 选择的工作日的index
     */
    selectedWorkday: [],
    /**
     * 选择的工作班次
     */
    selectedWorktime: '全天',
    /**
     * 资源类型选项
     */
    resTypeOptions: [
      '班组',
      '线体'
    ],
    /**
     * 工作日选择器的数据
     */
    workdayPickerData: [{
      index: 0,
      name: "一",
      checked: false
    }, {
      index: 1,
      name: "二",
      checked: false
    }, {
      index: 2,
      name: "三",
      checked: false
    }, {
      index: 3,
      name: "四",
      checked: false
    }, {
      index: 4,
      name: "五",
      checked: false
    }, {
      index: 5,
      name: "六",
      checked: false
    }, {
      index: 6,
      name: "日",
      checked: false
    }],
    /**
     * 工作班次选项
     */
    worktimeOptions: [
      '全天',
      '早班',
      '晚班'
    ],




    /**
     * 甘特图数据    
     * - load 负载率    
     * - backgroundColor 负载率对应颜色    
     * - products 排程中的产品    
     * - - startTime 起始时间 - 当天7:00    
     * - - endTime 同上    
     * - - color 产品对应背景颜色    
     * - 注，除了有产品的部分，空闲时间用空白块填满，保证products中的时间总和为24   
     * - 空白块实例: {    
     * - - productId: '',    
     * - - startTime: any,
     * - - endTime: any,
     * - - color: 'none'
     * - - }
     */
    gantData: [{
      id: 1234,
      name: 'line1',
      load: 0.33,
      backgroundColor: '#65a3ff',
      products: [{
        productId: 414837,
        startTime: 0,
        endTime: 4,
        color: '#89deff'
      }, {
        productId: '',
        startTime: 4,
        endTime: 16,
        color: 'none'
      }, {
        productId: 414837,
        startTime: 16,
        endTime: 20,
        color: '#89deff'
      }, {
        productId: '',
        startTime: 20,
        endTime: 24,
        color: 'none'
      }]
    }, {
      id: 1234,
      name: 'line2',
      load: 0.67,
      backgroundColor: '#fdc166',
      products: [{
        productId: '',
        startTime: 0,
        endTime: 4,
        color: 'none'
      }, {
        productId: 414837,
        startTime: 4,
        endTime: 16,
        color: '#89deff'
      }, {
        productId: '',
        startTime: 16,
        endTime: 20,
        color: 'none'
      }, {
        productId: 414837,
        startTime: 20,
        endTime: 24,
        color: '#89deff'
      }]
    }, {
      id: 1234,
      name: '彭晓慧',
      load: 0.5,
      backgroundColor: '#bdffc3',
      products: [{
        productId: 414837,
        startTime: 0,
        endTime: 12,
        color: '#89deff'
      }, {
        productId: '',
        startTime: 12,
        endTime: 24,
        color: 'none'
      }]
    }, {
      id: 1234,
      name: '王树梅',
      load: 0.5,
      backgroundColor: '#bdffc3',
      products: [{
        productId: '',
        startTime: 0,
        endTime: 12,
        color: 'none'
      }, {
        productId: 414837,
        startTime: 12,
        endTime: 24,
        color: '#89deff'
      }]
    }],





    /**
     * 负载图数据    
     * - id 资源id    
     * - name 资源名    
     * - avgRatio 一周内的平均负载    
     * - ratios 每一天的负载率信息    
     * - color 平均负载率对应的颜色    
     * - - index 相对于其实日期的序号    
     * - - ratio 负载率    
     * - - color 负载率对应的颜色    
     * - 注：所有负载率保留2位小数
     */
    loadratioData: [{
      id: 0,
      name: '徐贤燕',
      avgRatio: 0.32,
      color: '#65a3ff',
      ratios: [{
        index: 0,
        ratio: 0,
        color: '#c1f6ff'
      }, {
        index: 1,
        ratio: 0.50,
        color: '#bdffc3'
      }, {
        index: 2,
        ratio: 0.50,
        color: '#bdffc3'
      }, {
        index: 3,
        ratio: 0.50,
        color: '#bdffc3'
      }, {
        index: 4,
        ratio: 0.50,
        color: '#bdffc3'
      }, {
        index: 5,
        ratio: 0.33,
        color: '#65a3ff'
      }, {
        index: 6,
        ratio: 0,
        color: '#c1f6ff'
      }]
    }, {
      id: 0,
      name: '谢春霞',
      avgRatio: 0.07,
      color: '#c1f6ff',
      ratios: [{
        index: 0,
        ratio: 0,
        color: '#c1f6ff'
      }, {
        index: 1,
        ratio: 0,
        color: '#c1f6ff'
      }, {
        index: 2,
        ratio: 0,
        color: '#c1f6ff'
      }, {
        index: 3,
        ratio: 0,
        color: '#c1f6ff'
      }, {
        index: 4,
        ratio: 0.33,
        color: '#65a3ff'
      }, {
        index: 5,
        ratio: 0.17,
        color: '#c1f6ff'
      }, {
        index: 6,
        ratio: 0,
        color: '#c1f6ff'
      }]
    }, {
      id: 0,
      name: '陈梅',
      avgRatio: 0.05,
      color: '#c1f6ff',
      ratios: [{
        index: 0,
        ratio: 0,
        color: '#c1f6ff'
      }, {
        index: 1,
        ratio: 0,
        color: '#c1f6ff'
      }, {
        index: 2,
        ratio: 0,
        color: '#c1f6ff'
      }, {
        index: 3,
        ratio: 0.04,
        color: '#c1f6ff'
      }, {
        index: 4,
        ratio: 0.17,
        color: '#c1f6ff'
      }, {
        index: 5,
        ratio: 0.13,
        color: '#c1f6ff'
      }, {
        index: 6,
        ratio: 0,
        color: '#c1f6ff'
      }]
    }, {
      id: 0,
      name: '史义玲',
      avgRatio: 0.05,
      color: '#c1f6ff',
      ratios: [{
        index: 0,
        ratio: 0,
        color: '#c1f6ff'
      }, {
        index: 1,
        ratio: 0,
        color: '#c1f6ff'
      }, {
        index: 2,
        ratio: 0.04,
        color: '#c1f6ff'
      }, {
        index: 3,
        ratio: 0.13,
        color: '#c1f6ff'
      }, {
        index: 4,
        ratio: 0.17,
        color: '#c1f6ff'
      }, {
        index: 5,
        ratio: 0,
        color: '#c1f6ff'
      }, {
        index: 6,
        ratio: 0,
        color: '#c1f6ff'
      }]
    }, {
      id: 0,
      name: '许丽萍',
      avgRatio: 0.05,
      color: '#c1f6ff',
      ratios: [{
        index: 0,
        ratio: 0,
        color: '#c1f6ff'
      }, {
        index: 1,
        ratio: 0,
        color: '#c1f6ff'
      }, {
        index: 2,
        ratio: 0.17,
        color: '#c1f6ff'
      }, {
        index: 3,
        ratio: 0.17,
        color: '#c1f6ff'
      }, {
        index: 4,
        ratio: 0,
        color: '#c1f6ff'
      }, {
        index: 5,
        ratio: 0,
        color: '#c1f6ff'
      }, {
        index: 6,
        ratio: 0,
        color: '#c1f6ff'
      }]
    }, {
      id: 0,
      name: '高电压测试机',
      avgRatio: 0.26,
      color: '#65a3ff',
      ratios: [{
        index: 0,
        ratio: 0,
        color: '#c1f6ff'
      }, {
        index: 1,
        ratio: 0.38,
        color: '#65a3ff'
      }, {
        index: 2,
        ratio: 0.63,
        color: '#fdc166'
      }, {
        index: 3,
        ratio: 0.50,
        color: '#bdffc3'
      }, {
        index: 4,
        ratio: 0.17,
        color: '#c1f6ff'
      }, {
        index: 5,
        ratio: 0.17,
        color: '#c1f6ff'
      }, {
        index: 6,
        ratio: 0,
        color: '#c1f6ff'
      }]
    }, {
      id: 0,
      name: '自动包装机',
      avgRatio: 0.14,
      color: '#c1f6ff',
      ratios: [{
        index: 0,
        ratio: 0,
        color: '#c1f6ff'
      }, {
        index: 1,
        ratio: 0,
        color: '#c1f6ff'
      }, {
        index: 2,
        ratio: 0.17,
        color: '#c1f6ff'
      }, {
        index: 3,
        ratio: 0.20,
        color: '#65a3ff'
      }, {
        index: 4,
        ratio: 0.50,
        color: '#bdffc3'
      }, {
        index: 5,
        ratio: 0.13,
        color: '#c1f6ff'
      }, {
        index: 6,
        ratio: 0,
        color: '#c1f6ff'
      }]
    }, {
      id: 0,
      name: 'line35',
      avgRatio: 0.05,
      color: '#c1f6ff',
      ratios: [{
        index: 0,
        ratio: 0,
        color: '#c1f6ff'
      }, {
        index: 1,
        ratio: 0.17,
        color: '#c1f6ff'
      }, {
        index: 2,
        ratio: 0,
        color: '#c1f6ff'
      }, {
        index: 3,
        ratio: 0,
        color: '#c1f6ff'
      }, {
        index: 4,
        ratio: 0,
        color: '#c1f6ff'
      }, {
        index: 5,
        ratio: 0.17,
        color: '#c1f6ff'
      }, {
        index: 6,
        ratio: 0,
        color: '#c1f6ff'
      }]
    }],



    /**
     * 资源数据
     */
    resData: [{
      name: "line01",
      num: 1,
      type: "线体"
    }, {
      name: "line02",
      num: 1,
      type: "线体"
    }, {
      name: "line03",
      num: 1,
      type: "线体"
    }, {
      name: "line04",
      num: 1,
      type: "线体"
    }, {
      name: "line05",
      num: 1,
      type: "线体"
    }, {
      name: "line06",
      num: 1,
      type: "线体"
    }, {
      name: "line07",
      num: 1,
      type: "线体"
    }, {
      name: "line08",
      num: 1,
      type: "线体"
    }, {
      name: "line09",
      num: 1,
      type: "线体"
    }, {
      name: "line10",
      num: 1,
      type: "线体"
    }, {
      name: "line11",
      num: 1,
      type: "线体"
    }, {
      name: "line12",
      num: 1,
      type: "线体"
    }, {
      name: "line13",
      num: 1,
      type: "线体"
    }, {
      name: "line14",
      num: 1,
      type: "线体"
    }, {
      name: "line15",
      num: 1,
      type: "线体"
    }, {
      name: "line16",
      num: 1,
      type: "线体"
    }, {
      name: "line17",
      num: 1,
      type: "线体"
    }, {
      name: "line18",
      num: 1,
      type: "线体"
    }, {
      name: "line19",
      num: 1,
      type: "线体"
    }, {
      name: "line20",
      num: 1,
      type: "线体"
    }, {
      name: "line21",
      num: 1,
      type: "线体"
    }, {
      name: "line22",
      num: 1,
      type: "线体"
    }, {
      name: "line23",
      num: 1,
      type: "线体"
    }, {
      name: "line24",
      num: 1,
      type: "线体"
    }, {
      name: "line25",
      num: 1,
      type: "线体"
    }, {
      name: "line26",
      num: 1,
      type: "线体"
    }, {
      name: "line27",
      num: 1,
      type: "线体"
    }, {
      name: "line28",
      num: 1,
      type: "线体"
    }, {
      name: "line29",
      num: 1,
      type: "线体"
    }, {
      name: "line30",
      num: 1,
      type: "线体"
    }, {
      name: "line31",
      num: 1,
      type: "线体"
    }, {
      name: "line32",
      num: 1,
      type: "线体"
    }, {
      name: "line33",
      num: 1,
      type: "线体"
    }, {
      name: "line34",
      num: 1,
      type: "线体"
    }, {
      name: "line35",
      num: 1,
      type: "线体"
    }, {
      name: "line36",
      num: 1,
      type: "线体"
    }, {
      name: "line37",
      num: 1,
      type: "线体"
    }, {
      name: "line38",
      num: 1,
      type: "线体"
    }, {
      name: "line39",
      num: 1,
      type: "线体"
    }, {
      name: "line40",
      num: 1,
      type: "线体"
    }, {
      name: "line41",
      num: 1,
      type: "线体"
    }, {
      name: "line42",
      num: 1,
      type: "线体"
    }, {
      name: "line43",
      num: 1,
      type: "线体"
    }, {
      name: "弹片机",
      num: 3,
      type: "线体"
    }, {
      name: "电阻测试机",
      num: 2,
      type: "线体"
    }, {
      name: "移印机",
      num: 1,
      type: "线体"
    }, {
      name: "自动包装机",
      num: 2,
      type: "线体"
    }, {
      name: "高电压测试机",
      num: 5,
      type: "线体"
    }, {
      name: "10组-陈  云（5）",
      num: 5,
      type: '班组'
    }, {
      name: "11组-张  娟（4）",
      num: 4,
      type: "班组"
    }, {
      name: "12组-姚先兰（5）",
      num: 5,
      type: "班组"
    }, {
      name: "13组-刘金燕（3）",
      num: 3,
      type: "班组"
    }, {
      name: "14组-周  清（4）",
      num: 4,
      type: "班组"
    }, {
      name: "13组-刘金燕（3）",
      num: 3,
      type: "班组"
    }, {
      name: "15组-李长娟（5）",
      num: 5,
      type: "班组"
    }, {
      name: "16组-朱绍美（4）",
      num: 4,
      type: "班组"
    }, {
      name: "17组-吴桂凤（4）",
      num: 4,
      type: "班组"
    }, {
      name: "18组-芮  娜（3）",
      num: 3,
      type: "班组"
    }, {
      name: "19组-王  秀（3）",
      num: 3,
      type: "班组"
    }, {
      name: "1组-彭晓慧 (5)",
      num: 5,
      type: "班组"
    }, {
      name: "20组-王树梅（4）",
      num: 4,
      type: "班组"
    }, {
      name: "21组-陈荷花（4）",
      num: 4,
      type: "班组"
    }, {
      name: "22组-陈开霞（6）",
      num: 6,
      type: "班组"
    }, {
      name: "23组-吴永凤（4）",
      num: 4,
      type: "班组"
    }, {
      name: "24组-张 娟1（3）",
      num: 3,
      type: "班组"
    }, {
      name: "26组-杨  丽（5）",
      num: 5,
      type: "班组"
    }, {
      name: "27组-徐贤燕(5)",
      num: 5,
      type: "班组"
    }, {
      name: "28组-杜碧珍(4)",
      num: 4,
      type: "班组"
    }, {
      name: "30组-陈  梅(4)",
      num: 4,
      type: "班组"
    }, {
      name: "31组-史义玲(4)",
      num: 4,
      type: "班组"
    }, {
      name: "33组-许丽萍（5）",
      num: 5,
      type: "班组"
    }, {
      name: "36组-谢春霞(4)",
      num: 4,
      type: "班组"
    }, {
      name: "2组-丁雪梅（3）",
      num: 3,
      type: "班组"
    }, {
      name: "39组-刘  霞（3）",
      num: 3,
      type: "班组"
    }, {
      name: "2组-丁雪梅（3）",
      num: 3,
      type: "班组"
    }, {
      name: "4组-赵东勤（3）",
      num: 3,
      type: "班组"
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 显示/刷新甘特图
   */
  showGant: function (e) {
    console.log ("show gant")
    this.setData ({
      enableGant: true,
      enableLoad: false,
      enableManage: false
    })
  },

  /**
   * 显示/刷新负载图
   */
  showLoad: function(e) {
    console.log ("show load")
    this.setData ({
      enableGant: false,
      enableLoad: true,
      enableManage: false
    })
  },

  /**
   * 显示/刷新管理页面
   */
  showManage: function (e) {
    console.log ("show manage")
    this.setData ({
      enableGant: false,
      enableLoad: false,
      enableManage: true
    })
  },

  /**
   * 甘特图日期选择器的日期改变    
   * 更新甘特图
   */
  ondateChangeGant: function (e) {
    var date = e.detail.value
    console.log ("select date on gant: " + date)
    this.setData ({
      gantTimeSelected: date
    })
  },

  /**
   * 在甘特图点击前一天    
   * 更新甘特图
   */
  onclickPreDayGant: function (e) {
    var date = getPreDay(this.data.gantTimeSelected)
    console.log ("select date on gant: " + date)
    this.setData ({
      gantTimeSelected: date
    })
  },

  /**
   * 在甘特图点击下一天    
   * 更新甘特图
   */
  onclickNextDayGant: function (e) {
    var date = getNextDay(this.data.gantTimeSelected)
    console.log ("select date on gant: " + date)
    this.setData ({
      gantTimeSelected: date
    })
  },

  /**
   * 负载图日期选择器的日期改变    
   * 更新负载图
   */
  ondateChangeLoad: function (e) {
    var date = e.detail.value
    console.log ("select date on load: " + date)
    this.setData ({
      loadTimeSelected: date
    })
  },

  /**
   * 在负载图点击前一天    
   * 更新负载图
   */
  onclickPreDayLoad: function (e) {
    var date = getPreDay(this.data.loadTimeSelected)
    console.log ("select date on load: " + date)
    this.setData ({
      loadTimeSelected: date
    })
  },

  /**
   * 在负载图点击下一天    
   * 更新负载图
   */
  onclickNextDayLoad: function (e) {
    var date = getNextDay(this.data.loadTimeSelected)
    console.log ("select date on load: " + date)
    this.setData ({
      loadTimeSelected: date
    })
  },

  /**
   * 添加资源按钮的点击监听    
   * 打开添加资源框
   */
  onclickAddRes: function (e) {
    // this.setData({
    //   hideResAddForm: false,
    //   inputResName: '',
    //   selectedResType: '班组',
    //   inputResNum: 0,
    //   selectedWorkday: [],
    //   selectedWorktime: '全天',
    // })
    console.log('暂不支持添加')
  },

  /**
   * 确认添加资源按钮的点击监听    
   * // TODO: 请求后端添加资源
   */
  onclickConfirmAddForm: function (e) {
    
  },

  /**
   * 放弃添加资源按钮    
   * 关闭添加资源框
   */
  onclickCancelAddForm: function (e) {
    this.setData({
      hideResAddForm: true
    })
  },

  /**
   * 资源名输入框发生变化    
   * 更新已输入的资源名
   */
  onInputResNameChanged: function (e) {
    // TODO:
  },

  /**
   * 资源类型选择器发生变化    
   * 更新已经选择的类型
   */
  onTypeSelectorChanged: function (e) {
    var type = this.data.resTypeOptions[e.detail.value]
    console.log('res type picker: pick ' + type)
    this.setData({
      selectedResType: type
    })
  },

  /**
   * 输入的资源量发生变化    
   */
  onInputResNumChanged: function (e) {
    var num = e.detail.value
    console.log('input res num: ' + num)
    this.setData({
      inputResNum: num
    })
  },

  /**
   * 工作日选择器发生变化    
   */
  onWorkdayChanged: function (e) {
    var indexes = e.detail.value
    console.log(indexes)
    this.setData({
      selectedWorkday: indexes
    })
  }
})